<?php

class ipadActions extends myFrontendAction{

	public function executeProductSync(sfWebRequest $request){

		$q = Doctrine_Query::create()
			->select('p.id,')
			->from('Product p')
			//->limit(100)
			//->where('p.code  = ? OR p.code = ?', array(123,321))
			->execute();

		
		$pks = $q->getPrimaryKeys();

		//header('content-type: text/xml');
		
		$syncer = new ProductSyncer($pks);
		$res = $syncer->sync();
		echo $res;

		
		
		return sfView::NONE;
	}


	public function executePost(){
		print_r($_POST);
		exit;
	}

	public function executeCheckServer(sfWebRequest $request){
		return $this->renderPartial('global/json',
				array('item' => array('up'))
		);
	}

	public function executeGetScanned(sfWebRequest $request){
		$this->setLayout(false);


		$el = Doctrine::getTable('IpadQueue')->findOneByIpadId($this->getUser()->getIpadId());
		if($el){ // get product
			$prod = Doctrine::getTable('Product')->findOneByBarcode($el['product_barcode']);
			$el->delete();
		}

		if($prod){
			$result = array(
				'load_link' => $this->getComponent('linker','ipadProduct',array('id' => $prod['barcode'], 'module' => 'ipadProductPage' , 'action' => 'scannedPage')),
				'ipad_id' => $this->getUser()->getIpadId(),
				'code' => 302
			);
		}else{

			if($el){
				$result = array(
						'code' => 500,
						'ipad_id' => $this->getUser()->getIpadId(),
				);
			}else{
				$result = array(
						'code' => 404,
						'ipad_id' => $this->getUser()->getIpadId(),
				);
			}

		}

		
		return $this->renderPartial('global/json',
				array('item' => $result)
		);
	}
	

	public function executeFillQueue(sfWebRequest $request){

		echo "zzz";
		$this->setLayout(false);

		if($request->hasParameter('scanner') && $request->hasParameter('data')){
			Doctrine::getTable('IpadQueue')->createQuery('query cleaning')
					->delete('IpadQueue')
					->where('ipad_id = ?', $request->getParameter('scanner'))
					->execute();


			$this->newElement = new IpadQueue();
			$this->newElement->ipad_id = $request->getParameter('scanner');
			$this->newElement->product_barcode = $request->getParameter('data');

			$this->newElement->save();
		}

	}


	public function executeIdentifyIpad(sfWebRequest $request){
		$this->setLayout(false);
		$this->getUser()->setAttribute('ipad_id', $request->getParameter('ipad_id',false));

		$this->redirect($this->getComponent('linker', 'localizedHomepage'));
	}
	

	public function executeTest(){
		$this->setLayout('layout');
	}
	public function executeTest2(){
		$this->setLayout('layout');
	}
	public function executeTest3(){
		$this->setLayout('layout');
	}
	

	public function executeManager(sfWebRequest $request){
		$this->setLayout('layout_ipad');

		$this->ipadNumber = 26;
	}


	public function executeActiveIpads(sfWebRequest $request){
		$this->setLayout(false);

		$ipads = Doctrine::getTable('IpadsWaiting')->createQuery('active ipads')
						->select('iw.ipad_id as ipad_id')
						->from('IpadsWaiting iw')
						->setHydrationMode(Doctrine::HYDRATE_ARRAY)
						->execute();


		if($ipads){
			$result = array(
				'code' => 200,
				'ipads' => $ipads
			);
		}else{
			$result = array('code' => 404);
		}
		
		return $this->renderPartial('global/json', array('item' => $result));

	}
	

	public function executeActivate(sfWebRequest $request){
		$this->setLayout(false);
		
		$ipadWaiting = new IpadsWaiting();
		$ipadWaiting->ipad_id = $request->getParameter('ipad_id');

		$ipadWaiting->save();

		return sfView::NONE;
	}


	public function executeDelete(sfWebRequest $request){
		$this->setLayout(false);

		$el = Doctrine::getTable('IpadsWaiting')->createQuery('delete all active')
				->delete()
				->from('IpadsWaiting iw')
				->where('iw.ipad_id = ?', $request->getParameter('ipad_id'))
				->execute();

		return sfView::NONE;
	}
}
